import { TargetItem, TargetQuery, Pagination } from '@/views/account/targetManage/ModelDefines';
import { $http } from './index';

function queryParams(query: TargetQuery) {
    let year = 0;
    let month = 0;
    const components = query.date.split('-');
    if (components.length > 0) {
        year = +components[0];
    }
    if (components.length > 1) {
        month = +components[1];
    }
    const basic: Object = {
        joinTraderCode: query.groupCode,
        userNo: query.userCode,
        pageNum: query.pageNum,
        pageSize: query.pageSize,
    };
    if (+query.type === 1) {
        const params: Object = {
            annual: year,
        };
        return { ...basic, ...params };
    }
    if (+query.type === 2 || +query.type === 5) {
        const params: Object = {
            year: year,
            month: month,
        };
        return { ...basic, ...params };
    }
    if (+query.type === 4) {
        const params: Object = {
            year: year,
        };
        return { ...basic, ...params };
    }
    return basic;
}

// 搜索列表数据
export function searchTargets(query: TargetQuery) {
    if (+query.type === 1) {
        return $http.post('/saas/join-goal/annual/list', queryParams(query));
    }
    if (+query.type === 2) {
        return $http.post('/saas/join-goal/monthly/list', queryParams(query));
    }
    if (+query.type === 3) {
        return $http.post('/saas/join-goal/receivable/list', queryParams(query));
    }
    if (+query.type === 4 || +query.type === 5) {
        return $http.post('/saas/join-goal/published/list', queryParams(query));
    }
}

// 获取汇总信息
export function getTotals(query: TargetQuery) {
    if (+query.type === 1) {
        return $http.post('/saas/join-goal/annual/statistical', queryParams(query));
    }
    if (+query.type === 2) {
        return $http.post('/saas/join-goal/monthly/statistical', queryParams(query));
    }
    if (+query.type === 3) {
        return $http.post('/saas/join-goal/receivable/statistical', queryParams(query));
    }
    if (+query.type === 4 || +query.type === 5) {
        return $http.post('/saas/join-goal/published/statistical', queryParams(query));
    }
}

// 修改目标签约金额
export function editGoal(type: string, data: TargetItem) {
    if (+type === 1) {
        const params: Object = {
            annualResultsId: data.id,
            annualSigningTargets: data.goalSignAmount,
        };
        return $http.put('/saas/join-goal/annual', params);
    }
    if (+type === 2) {
        const params: Object = {
            monthlyResultsId: data.id,
            monthlySigningTargets: data.goalSignAmount,
        };
        return $http.put('/saas/join-goal/monthly', params);
    }
}

// 新增目标签约金额
export function addGoal(type: string, data: TargetItem) {
    if (+type === 1) {
        const params: Object = {
            annualSigningTargets: data.goalSignAmount,
            userCode: data.userCode,
            joinTraderCode: data.joinTraderCode,
            annual: data.year,
        };
        return $http.post('/saas/join-goal/annual', params);
    }
    if (+type === 2) {
        const params: Object = {
            monthlySigningTargets: data.goalSignMouthAmount,
            year: data.year,
            month: data.month,
            userCode: data.userCode,
            joinTraderCode: data.joinTraderCode,
        };
        return $http.post('/saas/join-goal/monthly', params);
    }
}
// 导入数据
export function importTargets(type: string, params: Object) {
    if (+type === 1) {
        return $http.post('/saas/join-goal/annual/import', params);
    }
    if (+type === 2) {
        return $http.post('/saas/join-goal/monthly/import', params);
    }
}

// 下载模板
export function downloadTemplate(type: string) {
    // configType 306-年度 307-月度
    return $http.get('/saas/join-goal/config/find-type?configType=' + (+type + 305));
}
// 查询用户列表
export function userList(params: {}) {
    return $http.get('/saas/join-goal/list-user', { params });

    // return $http.get('/saas//premisesDraft/downloadTemplate', { params });
}

// 下载数据
export function downloadData(query: TargetQuery) {
    if (+query.type === 1) {
        return $http.post('/saas/join-goal/annual/export', queryParams(query));
    }
    if (+query.type === 2) {
        return $http.post('/saas/join-goal/monthly/export', queryParams(query));
    }
    if (+query.type === 3) {
        return $http.post('/saas/join-goal/receivable/export', queryParams(query));
    }
    if (+query.type === 4 || +query.type === 5) {
        return $http.post('/saas/join-goal/published/export', queryParams(query));
    }
}

// 获取操作日志列表数据
export function getLogs(pagination: Pagination, query: TargetQuery) {
    const params: Object = {
        operationType: query.type,
        joinTraderCode: query.groupCode,
        pageNum: pagination.pageNum,
        pageSize: pagination.pageSize,
    };
    return $http.post('/saas/join-goal/log/list', params);
}
